package cn.ligoo.dtu.web.springmvc.platform;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import cn.ligoo.dtu.domain.BmuSlaveBalanceCurrent;
import cn.ligoo.dtu.domain.BmuSlaveBatteryState;
import cn.ligoo.dtu.domain.BmuSlaveBoxTemp;
import cn.ligoo.dtu.domain.BmuSlaveInfo;
import cn.ligoo.dtu.domain.UserInfo;
import cn.ligoo.dtu.service.impl.FacadeImpl;
import cn.ligoo.dtu.web.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/platform/bmu-slave-info")
public class BmuSlaveInfoController {
	private final Logger logger = LoggerFactory.getLogger(BmuSlaveInfoController.class);

	@Resource
	private FacadeImpl facade;

	public BmuSlaveInfoController() {
		logger.debug("...constructor");
	}

	/**
	 * 从机信息
	 * 
	 * @param request
	 * @param page
	 * @param rows
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/search", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String search(HttpServletRequest request, @RequestParam int page, @RequestParam int rows, BmuSlaveInfo t) {
		logger.debug("...search");

		logger.debug("...page:{}, rows:{}", page, rows);
		logger.debug("...bmuSlave:{}", t.toString());

		int total = facade.getBmuSlaveInfoService().getBmuSlaveInfoCount(t);

		t.getRow().setOffset((page - 1) * rows);
		t.getRow().setLimit(rows);
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		if (null != userInfo && null != userInfo.getIs_admin() && 1 != userInfo.getIs_admin()) {
			t.getMap().put("user_id", userInfo.getId()); // 如果不是管理员，传入user_id，取得此user_id对应的所有modem_id
		}
		List<BmuSlaveInfo> bmuSlaveList = facade.getBmuSlaveInfoService().getBmuSlaveInfoList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(bmuSlaveList), total);

		logger.debug("...json:{}", json);
		return json;
	}

	/**
	 * 电池状态（电压，均衡状态）
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/search-state", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchState(HttpServletRequest request, Integer bmu_slave_id) {
		logger.debug("...searchState");

		BmuSlaveBatteryState t = new BmuSlaveBatteryState();
		t.setBmu_slave_id(bmu_slave_id);
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		if (null != userInfo && null != userInfo.getIs_admin() && 1 != userInfo.getIs_admin()) {
			t.getMap().put("user_id", userInfo.getId()); // 如果不是管理员，传入user_id，取得此user_id对应的所有modem_id
		}
		List<BmuSlaveBatteryState> bsbsList = facade.getBmuSlaveBatteryStateService().getBmuSlaveBatteryStateList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(bsbsList), bsbsList.size());
		logger.debug("...json:{}", json);
		return json;
	}

	/**
	 * 均衡电流
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/search-current", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchCurrent(HttpServletRequest request, Integer bmu_slave_id) {
		logger.debug("...searchCurrent");

		BmuSlaveBalanceCurrent t = new BmuSlaveBalanceCurrent();
		t.setBmu_slave_id(bmu_slave_id);
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		if (null != userInfo && null != userInfo.getIs_admin() && 1 != userInfo.getIs_admin()) {
			t.getMap().put("user_id", userInfo.getId()); // 如果不是管理员，传入user_id，取得此user_id对应的所有modem_id
		}
		List<BmuSlaveBalanceCurrent> bsbcList = facade.getBmuSlaveBalanceCurrentService()
				.getBmuSlaveBalanceCurrentList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(bsbcList), bsbcList.size());
		logger.debug("...json:{}", json);
		return json;
	}

	/**
	 * 箱体温度
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/search-boxtemp", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchBoxTemp(HttpServletRequest request, Integer bmu_slave_id) {
		logger.debug("...searchBoxTemp");

		BmuSlaveBoxTemp t = new BmuSlaveBoxTemp();
		t.setBmu_slave_id(bmu_slave_id);
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		if (null != userInfo && null != userInfo.getIs_admin() && 1 != userInfo.getIs_admin()) {
			t.getMap().put("user_id", userInfo.getId()); // 如果不是管理员，传入user_id，取得此user_id对应的所有modem_id
		}
		List<BmuSlaveBoxTemp> bsbtList = facade.getBmuSlaveBoxTempService().getBmuSlaveBoxTempList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(bsbtList), bsbtList.size());
		logger.debug("...json:{}", json);
		return json;
	}
}
